package org.epidroid.epidroid.androsoul.engine.contacts;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import org.epidroid.epidroid.androsoul.engine.AndroSoul;

import android.util.Log;

/**
 * @author conrath
 * Saver for the contacts list
 */
public class ContactsSaver {
	private FileOutputStream _fos;
	ObjectOutputStream _oos;
	
	public ContactsSaver(FileOutputStream fos) {
		this._fos = fos;
		try {
			_oos = new ObjectOutputStream(fos);
		}
		catch (IOException e){
			Log.v(AndroSoul.LOGCAT, e.getMessage());
		}
	}
	
	public void close() {
		try {
			_oos.close();
			_fos.close();
		}
		catch (IOException e) {
			Log.v(AndroSoul.LOGCAT, e.getMessage());
		}
	}
	
	public void finalize() {
		close();
	}
	
	/**
	 * Write the contacts list in the file
	 * The contacts must implement the Serializable interface
	 */
	public void save(Contacts c) {
		try {
			_oos.writeObject(c);
			close();
		}
		catch (IOException e) {
			Log.v(AndroSoul.LOGCAT, e.getMessage());
		}
	}
}
